﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.ComponentModel;
using System.Drawing;
using System.Workflow.Activities;

namespace OpenComposite.Workflow
{
	[Designer(typeof(HorizontalEventHandlingScopeDesigner))]
	[ToolboxBitmap(typeof(resfinder), "OpenComposite.Workflow.Resources.eventbased.PNG")]
	[ToolboxItem(typeof(EventBasedXOrActivityToolboxItem))]
	public class HorizontalEventHandlingScope : EventBasedXOrActivity
	{
	}

	[ActivityDesignerTheme(typeof(HorizontalEventHandlingScopeDesignerTheme))]
	public class HorizontalEventHandlingScopeDesigner : ActivityPreviewDesigner
	{
		public HorizontalEventHandlingScopeDesigner()
		{
		}

		public override bool CanInsertActivities(HitTestInfo insertLocation, System.Collections.ObjectModel.ReadOnlyCollection<Activity> activitiesToInsert)
		{
			if ( activitiesToInsert.Count > 0 && activitiesToInsert[0] is EventDrivenBranch ) return true;
			else return false;
			//return base.CanInsertActivities(insertLocation, activitiesToInsert);
		}

		protected override void Initialize(Activity activity)
		{
			base.Initialize(activity);
			base.ShowPreview = false;
			if ( this.ContainedDesigners.Count == 0 ) {
				HorizontalEventHandlingScope act = this.Activity as HorizontalEventHandlingScope;
				if ( act == null ) return;
				act.Activities.Add(new EventDrivenBranch());
			}
		}

		protected override void OnPaint(ActivityDesignerPaintEventArgs e)
		{
			base.OnPaint(e);

		}

		protected override Rectangle ImageRectangle
		{
			get { return Rectangle.Empty; }
		}

		public override bool CanExpandCollapse
		{
			get { return false; }
		}

		protected override void OnLayoutPosition(ActivityDesignerLayoutEventArgs e)
		{
			base.OnLayoutPosition(e);
		}

		protected override Size OnLayoutSize(ActivityDesignerLayoutEventArgs e)
		{
			if ( this.Activity.QualifiedName != this.Activity.GetType().Name ) {
				//if ( ContainedDesigners.Count == 0 ) {
				//    List<Activity> list = new List<Activity>();
				//    list.Add(new EventDrivenBranch());
				//    InsertActivities(HitTest(Location), list.AsReadOnly());
				//}
			}
			return base.OnLayoutSize(e);
		}

		protected override void OnActivityChanged(ActivityChangedEventArgs e)
		{
			base.OnActivityChanged(e);
		}

		public override void RemoveActivities(System.Collections.ObjectModel.ReadOnlyCollection<Activity> activitiesToRemove)
		{
			CompositeActivity act = this.Activity as CompositeActivity;
			if ( act.Activities.Count == 1 ) {
				List<Activity> list = new List<Activity>();
				list.Add(new EventDrivenBranch());
				InsertActivities(new HitTestInfo(this, HitTestLocations.ActionArea),
					list.AsReadOnly());
			}
			base.RemoveActivities(activitiesToRemove);
		}

		protected override void OnContainedActivitiesChanging(ActivityCollectionChangeEventArgs listChangeArgs)
		{
			//    if ( listChangeArgs.RemovedItems != null && listChangeArgs.RemovedItems.Count > 0 ) {
			//        if ( this.ContainedDesigners.Count == listChangeArgs.RemovedItems.Count ) {
			//            //HorizontalEventHandlingScope activity = this.Activity as HorizontalEventHandlingScope;
			//            //for ( int i = 0; i < listChangeArgs.RemovedItems.Count; i++ ) {
			//            //    activity.Activities.Add(new EventDrivenBranch());
			//            //}
			//            List<Activity> list = new List<Activity>();
			//            list.Add(new EventDrivenBranch());
			//            InsertActivities(new HitTestInfo(this, HitTestLocations.Designer),
			//                list.AsReadOnly());
			//        }
			//    }
			base.OnContainedActivitiesChanging(listChangeArgs);
		}
	}

	public class HorizontalEventHandlingScopeDesignerTheme : ActivityPreviewDesignerTheme
	{
		public HorizontalEventHandlingScopeDesignerTheme(WorkflowTheme theme)
			: base(theme)
		{
			this.BorderColor = Color.Gray;
			this.BackColorStart = Color.FromArgb(53, 255, 255, 176);
			this.BackColorEnd = Color.FromArgb(53, 255, 255, 176);
			this.BorderStyle = System.Drawing.Drawing2D.DashStyle.Dash;
			this.BackgroundStyle = System.Drawing.Drawing2D.LinearGradientMode.ForwardDiagonal;
			this.ForeColor = Color.Black;
		}
	}
}
